package com.sh.service;


import com.sh.bean.CartItem;
import com.sh.bean.Resfood;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.io.StringWriter;
import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;


@Component
public class VelocityTemplateBizImpl {

    @Autowired
    MailBiz mailBiz;
    @Autowired
    private VelocityContext context;
    @Autowired
    @Qualifier("fullDf")
    DateFormat fullDf;
    @Autowired
    @Qualifier("partDf")
    DateFormat partDf;
    @Autowired
    Template Templete;

    public String genEmailContent(String username, Map<String, CartItem> cartItemMap, String userId){
        Date date = new Date();
        context.put("username", username);
        CartItem cartItem = cartItemMap.get(userId);
//        Resfood food = cartItem.getFood();
        Collection<CartItem> values = cartItemMap.values();
        StringBuffer fname = new StringBuffer();
        for (CartItem value : values) {
            String temp = value.getFood().getFname();
            fname.append(temp).append(" ");
        }
//        String fname = food.getFname();
        context.put("foodname", fname);
        context.put("optime", fullDf.format(date));
        context.put("currentDate", partDf.format(date));
        try (
                StringWriter writer = new StringWriter();
        ) {
            Templete.merge(context, writer);
            return writer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


}
